<template>
  <div class="userLogin">
    <h2>用户登录</h2>
    <a-space
      direction="vertical"
      size="large"
      :style="{ width: '360px' }"
      @submit="handleSubmit"
    >
      <a-form :model="form" :layout="layout">
        <a-form-item field="userAccount" label="用户账户">
          <a-input
            v-model="form.userAccount"
            placeholder="请输入您的用户账户"
          />
        </a-form-item>
        <a-form-item field="userPassword" label="用户密码">
          <a-input-password
            v-model="form.userPassword"
            placeholder="请输入您的密码"
          />
        </a-form-item>
        <a-form-item>
          <a-button html-type="submit" style="width: 100px">登录</a-button>
        </a-form-item>
      </a-form>
      没有账号请先注册
      <a-button @click="handleRegister" type="outline">注册</a-button>
    </a-space>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";

const layout = ref("vertical");
/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);

const router = useRouter();
const store = useStore();
/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    message.success("登录成功" + JSON.stringify(res.data.userAccount));
    await router.push({
      path: "/",
      replace: true,
    });
    await store.dispatch("user/getLoginUser");
  } else {
    message.error("登录失败" + res.message);
  }
};

const handleRegister = () => {
  router.push({
    path: "/user/register",
    replace: true,
  });
};
</script>
